package com.web.base.user.dao;

import org.springframework.stereotype.Repository;

import com.blue.jdbc.BaseDao;
import com.web.base.user.model.User;

/**
 * 用户数据库访问
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2017年04月16日
 */
@Repository
public class UserDao extends BaseDao<User>
{
	public UserDao()
	{
	}
	
	@Override
	protected void query(StringBuilder sql, User param)
	{
		if (param != null)
		{
			if (param.getName() != null && !param.getName().isEmpty())
			{
				sql.append(" and a.name like :name");
				param.setName("%" + param.getName() + "%");
			}
			
			if (param.getMobile() != null && !param.getMobile().isEmpty())
			{
				sql.append(" and a.mobile like :mobile");
				param.setMobile("%" + param.getMobile() + "%");
			}
			
			if (param.getNickname() != null && !param.getNickname().isEmpty())
			{
				sql.append(" and a.nickname like :nickname");
				param.setNickname("%" + param.getNickname() + "%");
			}
			
			if (param.getRole() != null)
				sql.append(" and a.role=:role");
				
			if (param.getAuth() != null)
				sql.append(" and a.auth=:auth");
				
		}
		sql.append(" order by a.id desc");
	}
	
	public User login(User user)
	{
		StringBuilder sql = new StringBuilder(32);
		sql.append("select * from uc_user where ");
		if (user.getName() != null && !user.getName().isEmpty())
		{
			sql.append("name=:name");
		}
		else if (user.getMobile() != null && !user.getMobile().isEmpty())
		{
			sql.append("mobile=:mobile");
		}
		else if (user.getEmail() != null && !user.getEmail().isEmpty())
		{
			sql.append("email=:email");
		}
		sql.append(" and password=:password");
		return jdbcObjectTemplate.get(sql.toString(), user);
	}
	
}
